package com.gemme.gemme.view;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gemme.gemme.R;
import com.gemme.gemme.shequ.BrandFragment;
import com.gemme.gemme.util.DpUtil;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

import java.util.ArrayList;
import java.util.List;

public class ABCView extends RelativeLayout {
    private Context mContext;
    public ABCView(Context context) {
        this(context,null);
    }

    public ABCView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ABCView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView();
    }

    private SwipeRecyclerView mRecyclerView;
    ArrayList<String> dataList = new ArrayList<>();
    private void initView() {
        LayoutInflater.from(mContext).inflate(R.layout.abc_view,this);
        mRecyclerView = findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));

        String arr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        for (int i = 0; i < arr.length(); i++) {
            dataList.add(arr.substring(i,i + 1));
        }

        System.out.println("");
        mRecyclerView.setAdapter(new ABCAdapter(dataList));

        initListener();

    }

    private void initListener() {


        mRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
            @Override
            public boolean onInterceptTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {
                if(e.getAction() == MotionEvent.ACTION_MOVE){
                    System.out.println("");
                    float y = e.getY();

                    getSelect(y);
                }
                return false;
            }

            @Override
            public void onTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {
                System.out.println("");
            }

            @Override
            public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
                System.out.println("");
            }
        });
    }
    String selecText = "";
    private void getSelect(float y) {
        try {
            int sdiff = DpUtil.dp2px((Activity) mContext, 12);
            int index = (int)(y / sdiff);
            String curent_s = dataList.get(index);
            if(TextUtils.isEmpty(curent_s))
                return;

            if(TextUtils.isEmpty(selecText)){
                selecText = curent_s;
            }else if(selecText.equals(curent_s)){//字段相同,不处理
                return;
            }

            System.out.println("index text : " + dataList.get(index) );
        }catch (Exception e){

        }


    }


    class ABCAdapter extends RecyclerView.Adapter<ABCAdapter.ViewHolder>{

        private List<String> adapterList;

        public ABCAdapter(List<String> adapterList){

            this.adapterList = adapterList;
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = LayoutInflater.from(parent.getContext()).
                    inflate(R.layout.abc_item, null);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            inflate.setLayoutParams(layoutParams);

            return new ABCAdapter.ViewHolder(inflate);

        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            try {
                holder.name.setText(adapterList.get(position));
                System.out.println("");
            }catch (Exception e){

            }

        }

        @Override
        public int getItemCount() {
            return adapterList.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            TextView name;
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                name = itemView.findViewById(R.id.name);
            }
        }
    }

}
